<?php

function json($success, $message = "", $data = array())
{
  $result = array(
    'success' => $success,
    'message' => urlencode($message),
    'docs' => $data
  );  
  //输出json  
  echo urldecode(json_encode($result));
}



$body = json_decode(file_get_contents('php://input'),true);
$email = $body['email'];
$userpassword = md5($body['password']);


$servername = "localhost";
$username = "root";
$password = "";
$dbName = 'taodb';
$tbnameA = 'users';
$tbnameB = "collect";

$mysqli = new mysqli($servername, $username, $password, $dbName);

if ($mysqli->connect_error) {
  die("连接失败: " . $mysqli->connect_error);
}

$data =array();
// $email and a.password = $password 
$sql = "SELECT a.username,a.email,a.id,b.collect FROM $tbnameA a INNER JOIN $tbnameB b ON a.collectid = b.id WHERE a.email = '$email' and a.password = '$userpassword' ";
$result = $mysqli->query($sql);
if ($result) {
  while($row = $result->fetch_assoc()){  
    $data = $row; 
    $data['username'] = urlencode($row['username']);
    $data['collect'] = explode(",",$row['collect']);
  }
  json('true', '登录成功', $data);
}else{
  json('false', '登录失败', $data);
}


$mysqli->close();
// var_dump($result);